package com.li.utilstest

import android.R.attr
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.media.projection.MediaProjectionManager
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.provider.Contacts.SettingsColumns.KEY
import android.util.Log
import android.view.Surface
import android.view.SurfaceView
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ScreenUtils
import com.li.utilstest.MediaRecordScreenService.Companion.START
import com.li.utilstest.MediaRecordScreenService.Companion.STOP
import kotlinx.android.synthetic.main.activity_screen_record.*
import java.io.File


class ScreenRecordActivity : AppCompatActivity() {
    var surface: Surface? = null
    var mediaManager: MediaProjectionManager? = null
    var mediaIntent: Intent? = null
    var binder: MediaRecordServer.MyBinder? = null
    var mMediaRecordService: MediaRecordService? = null

    var serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            binder = service as MediaRecordServer.MyBinder?
        }

        override fun onServiceDisconnected(name: ComponentName?) {
        }

    }
    @RequiresApi(Build.VERSION_CODES.O)
    var launder = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
        // 获取申请录屏结果
        if (it.resultCode == RESULT_OK) {

            var intent = Intent(this, MediaRecordScreenService::class.java)
            intent.putExtra(MediaRecordScreenService.KEY, START)
            intent.putExtra("data", it.data)
            intent.putExtra("resultCode", it.resultCode)
            intent.putExtra("width", ScreenUtils.getScreenWidth()) // 屏幕的宽
            intent.putExtra("height", ScreenUtils.getScreenHeight()) // 屏幕的高
            startForegroundService(intent) // 启动前台服务

        }
    }
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_screen_record)
        surface =  surface_view.holder.surface
        initData()
    }

    /**
     * 申请权限
     */
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun requestScreenRecorder() {
        mediaManager =
            getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager?
        mediaIntent = mediaManager?.createScreenCaptureIntent()
        launder.launch(mediaIntent)
    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun initData() {
        start.setOnClickListener {
            if(!PermissionUtils.isGranted(PermissionConstants.MICROPHONE,PermissionConstants.STORAGE)){
                PermissionUtils.permission(PermissionConstants.MICROPHONE,PermissionConstants.STORAGE)
            }else {
                PermissionUtils.permission(PermissionConstants.MICROPHONE,PermissionConstants.STORAGE)
                requestScreenRecorder()
            }
        }
        stop.setOnClickListener {
            var intent = Intent(this, MediaRecordScreenService::class.java)
            intent.putExtra(MediaRecordScreenService.KEY, STOP)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(intent)
            } // 启动前台服务
        }
    }


}